<template>
  <view class="dessert-container">
    <scroll-view scroll-y="true" class="dessert-left" enable-flex="true">
      <view
        v-for="(item, index) in typeList"
        :class="['dessert-item', activeIndex === index ? 'active' : '']"
        :key="item.id"
        @click="
          changeActive(index);
          getDessertInfo(item.id)
        ">
        {{ item.typeName }}
      </view>
    </scroll-view>
    <scroll-view scroll-y="true" class="dessert-right">
      <view
        v-for="item in desserts"
        :key="item.id"
        :class="['dessert-content', item.status === 1 ? 'off-sale' : '']">
        <view class="content-left" @click="goDessertDetail(item.id)">
          <image
            :src="`http://localhost:8080/api/files/${item.dessertImg}`"
            mode="aspectFit"></image>
          <view class="dessert-text">
            <text class="dessert-name">{{ item.dessertName }}</text>
            <text class="dessert-specs">{{ '规格: ' + item.specs }}</text>
            <text class="dessert-count">{{ '销量: ' + item.sale }}</text>
            <text class="dessert-price">{{ '￥' + item.dessertPrice }}</text>
          </view>
        </view>
        <view class="book-btn">
          <text
            style="
              font-size: 22rpx;
              color: #666;
              width: 80rpx;
              margin-left: 20rpx;
            "
            v-if="item.status === 1">
            已下架
          </text>
          <up-button
            @click="goOrder(item)"
            text="预 订"
            color="#f9acb4"
            class="custom-style"
            size="mini"
            v-else></up-button>
        </view>
      </view>
    </scroll-view>
  </view>
</template>

<script setup>
import { ref } from 'vue'
import {
  getAllTypeService,
  getDessertByTypeService,
  getDessertSales
} from '../../api/dessert.js'
import { onShow, onHide } from '@dcloudio/uni-app'
import { useUserStore } from '../../stores/user.js'

const userStore = useUserStore()

// 获取甜品分类信息
const typeList = ref([])
const getTypeInfo = async () => {
  const res = await getAllTypeService()
  typeList.value = res.data
  getDessertInfo(typeList.value[0].id)
}
getTypeInfo()

// 点击切换左侧菜单项
const activeIndex = ref(0)
const changeActive = (index) => {
  activeIndex.value = index
}

// 选中发送请求 获取对应分类的甜品信息
const dessertList = ref([])
const desserts = ref([])
const getDessertInfo = async (typeId) => {
  desserts.value = []
  const res = await getDessertByTypeService(typeId)
  dessertList.value = res.data || []
  
  // 获取甜品的销量
  if(dessertList.value) {
    dessertList.value.forEach(async (item) => {
      const sale = await getDessertSales(item.id)
      item.sale = sale.data
      desserts.value.push(item)
    })
  }
}

// 点击跳转到甜品详情页
const goDessertDetail = (id) => {
  uni.navigateTo({
    url:
      '/pages/dessertDetail/dessertDetail?id=' +
      encodeURIComponent(JSON.stringify(id)),
  })
}
onShow(() => {
  uni.$on('update')
})
onHide(() => {
  uni.$off('update')
})

// 预订
const orderInfo = ref({})
const goOrder = (item) => {
  orderInfo.value.userId = userStore.userInfo.userId
  orderInfo.value.dessertId = item.id
  orderInfo.value.num = 1
  orderInfo.value.dessert = item
  // 跳转到订单页
  uni.navigateTo({
    url: `/pages/bookOrder/bookOrder?info=${encodeURIComponent(
      JSON.stringify(orderInfo.value)
    )}`,
  })
}
</script>

<style lang="scss" scoped>
.dessert-container {
  width: 100%;
  height: 100%;
  display: flex;
  border-top: 1px solid #ffdbdd;
  box-sizing: border-box;
  .dessert-left {
    width: 25%;
    height: 100%;
    background-color: #fff5f7;
    border-radius: 10rpx;
    box-sizing: border-box;
    .dessert-item {
      width: 100%;
      height: 80rpx;
      line-height: 80rpx;
      padding-left: 40rpx;
      box-sizing: border-box;
    }
    .active {
      background-color: #fff;
      position: relative;
      padding-left: 40rpx;
      box-sizing: border-box;
      &::before {
        content: '';
        display: block;
        width: 8rpx;
        border-radius: 6rpx;
        height: 50rpx;
        background-color: #f9acb4;
        position: absolute;
        left: 0;
        top: 18rpx;
      }
    }
  }
  .dessert-right {
    width: 74%;
    height: 980rpx;
    margin-left: 10rpx;
    overflow: hidden;
    .off-sale {
      position: relative;
      // 使用淡化效果
      filter: grayscale(100%);
      color: gray;
      // 点击事件失效
      pointer-events: none;
    }
    .dessert-content {
      position: relative;
      width: 100%;
      height: 180rpx;
      border-bottom: 1rpx solid #ffdbdd;
      display: flex;
      align-items: center;
      box-sizing: border-box;
      .content-left {
        width: 80%;
        display: flex;
        align-items: center;
        image {
          width: 140rpx;
          height: 140rpx;
        }
        .dessert-text {
          width: 300rpx;
          display: flex;
          flex-direction: column;
          margin-left: 10rpx;
          .dessert-name {
            font-size: 28rpx;
            font-weight: bold;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
          }
          .dessert-specs {
            font-size: 22rpx;
            color: #666;
            margin-top: 2rpx;
          }
          .dessert-count {
            font-size: 22rpx;
            color: #666;
            margin-top: 2rpx;
          }
          .dessert-price {
            font-size: 28rpx;
            color: #bb7755;
            margin-top: 16rpx;
          }
        }
      }
      .book-btn {
        display: flex;
        // flex-direction: column;
        font-size: 28rpx;
        width: 100rpx;
        margin-top: 120rpx;
        margin-right: 40rpx;
      }
    }
  }
}
</style>
